package model

import "gorm.io/gorm"

type User struct {
	gorm.Model
	Phone            string `gorm:"type:char(11)" json:"Phone"`
	Password         string `gorm:"type:varchar(30)" json:"password"`
	Username         string `gorm:"type:varchar(30)" json:"username"`
	AvatarUrl        string `gorm:"type:varchar(255)" json:"avatarUrl"`
	MemberId         int    `gorm:"type:int" json:"memberId"`
	MemberStatus     int    `gorm:"type:int" json:"memberStatus"`
	MemberExpireTime int    `gorm:"type:int" json:"memberExpireTime"`
}

func (u *User) IsUser(db *gorm.DB, userId int) error {
	return db.Where("id = ?", userId).First(&u).Error
}

func (u *User) IsPhone(db *gorm.DB, phone string) error {
	return db.Where("phone = ?", phone).First(&u).Error
}

func (u *User) Register(db *gorm.DB) error {
	return db.Create(&u).Error
}

func (u *User) UpdateExpireTime(db *gorm.DB, userId int, expireTime int) error {
	return db.Model(&u).Where("id = ?", userId).Update("member_expire_time", expireTime).Error
}
